﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace GraphDrawer
{
	public partial class NewNodePopUp : UserControl
	{
        public event RoutedEventHandler Closed;
        public event RoutedEventHandler OK;

		public NewNodePopUp()
		{
			// Required to initialize variables
			InitializeComponent();
            this.KeyDown += new KeyEventHandler(NewNodePopUp_KeyDown);
            Closed += new RoutedEventHandler(NewNodePopUp_Closed);
            OK += new RoutedEventHandler(NewNodePopUp_OK);
            var colors = ColorsToArray(typeof(Colors));
            
            foreach (var item in colors)
            {
                ComboBoxColors.Items.Add(item);
            }
            ComboBoxColors.SelectedIndex = 0;
		}

        void NewNodePopUp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                OK.Invoke(this, new RoutedEventArgs());
            }
            if (e.Key == Key.Escape)
            { 
                Closed.Invoke(this, new RoutedEventArgs());   
            }
        }

        public String NodeName
        {
            get 
            {
                return Name.Text;
            }
        }

        public Color NodeColor
        {
            get
            {
                return (Color)typeof(Colors).GetProperty(ComboBoxColors.SelectedItem.ToString()).GetValue(null, null);
            }
        }

        public String StateClass
        {
            get
            {
                return ComboBoxStateNames.SelectedItem.ToString();
            }
        }

        void NewNodePopUp_OK(object sender, RoutedEventArgs e)
        {
            
        }

        void NewNodePopUp_Closed(object sender, RoutedEventArgs e)
        {
            
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Collapsed;
            Closed.Invoke(sender, e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Collapsed;
            OK.Invoke(this, e);
        }

        public static String[] ColorsToArray(Type enumeration)
        {

            //get the enumeration type
            Type et = enumeration;

            //get the public static fields (members of the enum)
            System.Reflection.PropertyInfo[] fi = et.GetProperties(BindingFlags.Static | BindingFlags.Public);

            //create a new enum array
            String[] values = new String[fi.Length];

            //populate with the values
            for (int iEnum = 0; iEnum < fi.Length; iEnum++)
            {
                values[iEnum] = fi[iEnum].Name;

            }

            //return the array
            return values;

        }
	}
}